//
//  AudioListViewController.m
//  AudioBook_A
//
//  Created by Sansan on 15/4/16.
//  Copyright (c) 2015年 Sansan. All rights reserved.
//

#import "AudioListViewController.h"

@interface AudioListViewController ()

@end

@implementation AudioListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = [UIColor colorWithRed:207/255.0 green:234/255.0 blue:236/255.0 alpha:1.0];
    
    // 设置segment
    NSMutableArray *array = [NSMutableArray arrayWithObjects:@"最火", @"最近更新", @"经典", nil];
    // 分段按钮
    self.segment = [[UISegmentedControl alloc] initWithItems:array];
    self.segment.frame = CGRectMake(20, 72, ScreenFram.width - 40, 30);
    // 选中按钮的下标
    self.segment.selectedSegmentIndex = 0;
    // segment切换
    [self.segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    // 设置按钮颜色
    self.segment.tintColor = [UIColor orangeColor];
    [self.view addSubview:self.segment];
    
    UIImage *image = [UIImage imageNamed:@"back.png"];
    // 取消渲染
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(returnbackAction)];
    
    // 创建筛选按钮
    UIImage *screenImage = [UIImage imageNamed:@"screen.png"];
    // 取消渲染
    screenImage = [screenImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:screenImage style:UIBarButtonItemStylePlain target:self action:@selector(screenAction)];
    
    self.ListTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 108, ScreenFram.width, ScreenFram.height - 200)];
    self.ListTableView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.ListTableView];
    
    self.ListTableView.delegate = self;
    self.ListTableView.dataSource = self;
    self.ListTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.allModelArray = [NSMutableArray array];
    
    // 设置当前的第一个页数是1
    self.page = 1;
    
    // 2.集成刷新控件
    [self addHeader];
    [self addFooter];
    
    
    
    // Do any additional setup after loading the view.
}


#pragma mark --下拉刷新
- (void)addHeader
{
    //    __unsafe_unretained typeof(self) vc = self;
    __block AudioListViewController *vc = self;
    
    
    // 添加下拉刷新头部控件
    [self.ListTableView addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        // 在block块里实现afn数据请求, 一定要写在这里
        // [vc getAFNData];
        
        
        
        NSString *url = [NSString stringWithFormat:URLINTERFACE, self.urlKey, @"hot", 1, 0, self.tname];
        
        [MyAFNData getNetData:url responseGETData:^(id data) {
            NSMutableArray *array = [data objectForKey:@"list"];
            for (NSMutableDictionary *dic in array) {
                AudioListModel *model = [[AudioListModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.allModelArray addObject:model];
            }
            [vc.ListTableView headerEndRefreshing];
            [self.ListTableView reloadData];
        }];
    }];
    
#pragma mark  --自动刷新(一进入程序就下拉刷新)
    [self.ListTableView headerBeginRefreshing];
}

#pragma mark --上拉加载更多
- (void)addFooter
{
    //    __unsafe_unretained typeof(self) vc = self;
    __block AudioListViewController *vc = self;
    
    // 添加上拉刷新尾部控件
    [self.ListTableView addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        NSString *type = @"hot";
        if (self.segment.selectedSegmentIndex == 1) {
            type = @"recent";
        } else if (self.segment.selectedSegmentIndex == 2) {
            type = @"classic";
        }
        
        // 用afn加载更多数据
        NSString *url = [NSString stringWithFormat:URLINTERFACE, self.urlKey, type, (int)++self.page, 0, self.tname];
        [MyAFNData getNetData:url responseGETData:^(id data) {
            NSMutableArray *array = [data objectForKey:@"list"];
            for (NSMutableDictionary *dic in array) {
                AudioListModel *model = [[AudioListModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.allModelArray addObject:model];
                
            }
            [vc.ListTableView footerEndRefreshing];
            [self.ListTableView reloadData];
        }];
        
    }];
}

// 实现segment切换的触发方法
- (void)segmentAction:(UISegmentedControl *)segment
{
    NSLog(@"segment切换触发的方法");
    if (segment.selectedSegmentIndex == 0) {
        self.allModelArray = [NSMutableArray array];
        NSString *url = [NSString stringWithFormat:URLINTERFACE, self.urlKey, @"hot", 1, 0, self.tname];
        [MyAFNData getNetData:url responseGETData:^(id data) {
            NSMutableArray *array = [data objectForKey:@"list"];
            for (NSMutableDictionary *dic in array) {
                AudioListModel *model = [[AudioListModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.allModelArray addObject:model];
                
            }
            [self.ListTableView reloadData];
            
        }];
        
        
    }else if (segment.selectedSegmentIndex == 1){
        self.allModelArray = [NSMutableArray array];
        NSString *url = [NSString stringWithFormat:URLINTERFACE, self.urlKey, @"recent", 1, 0, self.tname];
        [MyAFNData getNetData:url responseGETData:^(id data) {
            NSMutableArray *array = [data objectForKey:@"list"];
            for (NSMutableDictionary *dic in array) {
                AudioListModel *model = [[AudioListModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.allModelArray addObject:model];
                
            }
            [self.ListTableView reloadData];
            
        }];
        
        
    }else if (segment.selectedSegmentIndex == 2){
        self.allModelArray = [NSMutableArray array];
        NSString *url = [NSString stringWithFormat:URLINTERFACE, self.urlKey, @"classic", 1, 0, self.tname];
        [MyAFNData getNetData:url responseGETData:^(id data) {
            NSMutableArray *array = [data objectForKey:@"list"];
            for (NSMutableDictionary *dic in array) {
                AudioListModel *model = [[AudioListModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.allModelArray addObject:model];
                
            }
            [self.ListTableView reloadData];
            
        }];
    }
}

// 两个必须实现的方法之一
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.allModelArray.count;
}



// 两个必须实现的方法之一
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndentiller = @"cell";
    AudioListTableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:cellIndentiller];
    if (cell == nil) {
        cell = [[AudioListTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIndentiller];
    }
    
    
    AudioListModel *model = [self.allModelArray objectAtIndex:indexPath.row];
    cell.model = model;
    
    
    // cell 点击后没有阴影效果
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    cell.contentView.tag = 1000 + indexPath.row;
    
    // 轻拍手势进入详情页面
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAvtion:)];
    [cell.contentView addGestureRecognizer:tap];
    cell.contentView.backgroundColor = [UIColor colorWithRed:207/255.0 green:234/255.0 blue:236/255.0 alpha:1.0];
    
    return cell;
}

//轻拍手势触发的方法
- (void)tapAvtion:(UITapGestureRecognizer *)tap
{
    DetailAlbumsViewController *audioVC = [[DetailAlbumsViewController alloc] init];
    NSInteger key = tap.view.tag % 1000;
    NSString *myID = ((AudioListModel *)[self.allModelArray objectAtIndex:key]).id;
    // 属性传值把id传递给下一个页面
    NSString *title = ((AudioListModel *)[self.allModelArray objectAtIndex:key]).title;
    audioVC.ID = myID;
    audioVC.title = title;
    [self.navigationController pushViewController:audioVC animated:YES];
    [audioVC release];
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}



// 触发筛选的方法
- (void)screenAction
{
    NSLog(@"筛选触发");
    
}


- (void)returnbackAction
{
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
